/**
 * Project Name:bfs_v1.0.1
 * File Name:RolePntenterServiceImpl.java
 * Package Name:com.zeei.bfs.service.impl
 * Date:2016年12月16日上午11:22:17
 * Copyright (c) 2016, ZEEI All Rights Reserved. *
 *
 * ClassName:RolePntenterServiceImpl 
 * Function: TODO ADD FUNCTION.  
 * @author   zws 
 * @since    JDK 1.8	 
 */

package com.zeei.bfs.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeei.bfs.dao.RolePntenterDao;
import com.zeei.bfs.service.RolePntenterService;
import com.zeei.common.vo.RolePntenterVO;
import com.zeei.common.vo.UserVO;
import com.zeei.common.constant.GlobalConstant;

@Service("rolePntenterService")
public class RolePntenterServiceImpl implements RolePntenterService {

	@Autowired
	private RolePntenterDao rolePntenterDao;

	@Override
	public boolean addRolePntenter(Integer roleId, List<String> objIds, Integer createId, String type) {
		if (roleId == null) {
			return GlobalConstant.FAILED;
		}
		if (objIds == null) {
			rolePntenterDao.deleteRolePntenterByRoleId(roleId);
			return GlobalConstant.SUCCEED;
		}
		rolePntenterDao.deleteRolePntenterByRoleId(roleId);
		List<RolePntenterVO> rolePntenterList = new ArrayList<RolePntenterVO>();
		for (int i = 0; i < objIds.size(); i++) {
			RolePntenterVO rolePntenterVO = new RolePntenterVO();
			rolePntenterVO.setRoleId(roleId);
			rolePntenterVO.setObjId(Integer.valueOf(objIds.get(i)));
			rolePntenterVO.setCreateId(createId);
			rolePntenterVO.setType(type);
			rolePntenterList.add(rolePntenterVO);
		}
		if (CollectionUtils.isNotEmpty(rolePntenterList)) {
			int N = GlobalConstant.DEFAULT_BATCH_SIZE;
			int size = rolePntenterList.size();
			int index = 0;
			int toIndex = N;
			while (toIndex < size) {
				rolePntenterDao.insertRolePntenterList(rolePntenterList.subList(index, toIndex));
				toIndex += N;
				index += N;
			}
			rolePntenterDao.insertRolePntenterList(rolePntenterList.subList(index, size));
		}
		return GlobalConstant.SUCCEED;
	}

	@Override
	public List<RolePntenterVO> queryRolePntenter(Integer roleId, String type, UserVO currentUser) {
		List<RolePntenterVO> resultList;
		if(GlobalConstant.ADMIN_NAME.equalsIgnoreCase(currentUser.getLoginname())){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("roleId", roleId);
			condition.put("type", type);
			resultList = rolePntenterDao.queryRolePntenterByCondition(condition);
		}else{
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("roleId", roleId);
			condition.put("type", type);
			condition.put("userId", currentUser.getUserid());
			resultList = rolePntenterDao.queryRolePntenterByCondition(condition);
		}
		return resultList;
	}

}
